//
//  LZLoginButtonView.m
//  laziz_Consumer
//
//  Created by ZhaoDongBo on 2016/10/24.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZLoginButtonView.h"
#import "Masonry.h"
#import "BlocksKit+UIKit.h"

@implementation LZLoginButtonView

- (instancetype)initWithPage:(LZLOGINBUTTONTYPE_PAGE)page {
    self = [super init];
    if (self) {
        self.page = page;
        [self setup];
    }
    return self;
}

- (void)setup {

    WS(weakSelf);

    if (self.page == LZLOGINBUTTONTYPE_PAGE_LOGIN) {
        [self addSubview:self.loginButton];
        [self addSubview:self.registerButton];
        
        [self.loginButton setTitle:LZLocalizedString(@"login_loginnow", nil) forState:UIControlStateNormal];
        
        [self.loginButton makeConstraints:^(MASConstraintMaker *make) {
            make.leading.mas_equalTo(30);
            make.height.mas_equalTo(44);
            make.trailing.mas_equalTo(-30);
            make.bottom.mas_equalTo(weakSelf.registerButton.mas_top).with.offset(-25);
        }];
        
        [self.registerButton makeConstraints:^(MASConstraintMaker *make) {
            make.leading.mas_equalTo(30);
            make.bottom.mas_equalTo(0);
            make.trailing.mas_equalTo(-30);
            make.height.mas_equalTo(44);
        }];
    }
    else if (self.page == LZLOGINBUTTONTYPE_PAGE_REGISTER) {
        
        [self addSubview:self.loginButton];
        
        [self.loginButton setTitle:LZLocalizedString(@"login_register", nil) forState:UIControlStateNormal];
        
        [self.loginButton makeConstraints:^(MASConstraintMaker *make) {
            make.leading.mas_equalTo(30);
            make.height.mas_equalTo(44);
            make.trailing.mas_equalTo(-30);
            make.bottom.mas_equalTo(0);
        }];
    }
}

#pragma mark - setter/getter

- (UIButton *)loginButton {
    if (_loginButton == nil) {
        
        WS(weakSelf);
        _loginButton = [[UIButton alloc] init];
        _loginButton.backgroundColor        = UIColorFromRGB(0x33a928, 1);
        _loginButton.tag                    = LZLOGINBUTTONTYPE_TAG_LOGIN;
        _loginButton.titleLabel.font        = [UIFont systemFontOfSize:17.0];
        _loginButton.layer.masksToBounds    = YES;
        _loginButton.layer.cornerRadius     = 44/2.0f;
    
        [_loginButton setTitleColor:UIColorFromRGB(0xfffefd, 1) forState:UIControlStateNormal];
        
        [_loginButton bk_addEventHandler:^(id  _Nonnull sender) {
            if (weakSelf.buttonActionBlock) {
                weakSelf.buttonActionBlock(LZLOGINBUTTONTYPE_TAG_LOGIN);
            }
        } forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _loginButton;
}

- (UIButton *)registerButton {

    if (_registerButton == nil) {
        
        WS(weakSelf);
        _registerButton = [[UIButton alloc] init];
        _registerButton.tag                 = LZLOGINBUTTONTYPE_TAG_REGISTER;
        _registerButton.titleLabel.font     = [UIFont systemFontOfSize:17.0];
        _registerButton.layer.masksToBounds = YES;
        _registerButton.layer.cornerRadius  = 44/2.0f;
        _registerButton.layer.borderWidth   = 1;
        _registerButton.layer.borderColor   = UIColorFromRGB(0x33a928, 1).CGColor;
        
        [_registerButton setTitleColor:UIColorFromRGB(0x33a928, 1) forState:UIControlStateNormal];
        [_registerButton setTitle:LZLocalizedString(@"login_register", nil) forState:UIControlStateNormal];
        
        [_registerButton bk_addEventHandler:^(id  _Nonnull sender) {
            if (weakSelf.buttonActionBlock) {
                weakSelf.buttonActionBlock(LZLOGINBUTTONTYPE_TAG_REGISTER);
            }
        } forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _registerButton;

}

@end
